package com.noahplat.user.controller;

import com.noahplat.common.utils.Result;
import com.noahplat.user.pojo.Role;
import com.noahplat.user.service.RoleService;
import com.noahplat.user.utils.RoleDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;


@RequestMapping("/api/role")
@RestController
@Validated
public class RoleController {
    @Autowired
    private RoleService roleService;

    /**
     * 添加角色
     */
    @PreAuthorize("hasAuthority('noahplat-service-user:RoleController:addRole')")
    @PostMapping("/addRole")
    public Result addRole(@RequestBody Role role){
        return roleService.addRole(role);
    }

    /**
     * 获取角色列表
     */
    @PreAuthorize("hasAuthority('noahplat-service-user:RoleController:getRole')")
    @GetMapping("/getRole")
    public Result getRole(@RequestParam(defaultValue = "1")Integer page,@RequestParam(defaultValue = "10")Integer limit,@NotNull(message = "账套ID不能为空 ") @Min(value = 1, message = "账套Id不能为空")Long acctid,@NotNull(message = "系统ID不能为空 ") @Min(value = 1, message = "系统Id不能为空")Long stid,String description){
        return roleService.getRole(page, limit, acctid,stid,description);
    }

    /**
     * 不分页获取角色列表
     */
    @PreAuthorize("hasAuthority('noahplat-service-user:RoleController:selectRole')")
    @GetMapping("/selectRole")
    public Result selectRole(Long acctid,Long stid){
        return roleService.selectRole(acctid, stid);
    }

    /**
     * 修改角色信息
     */
    @PreAuthorize("hasAuthority('noahplat-service-user:RoleController:updateRole')")
    @PutMapping("/updateRole")
    public Result updateRole(@RequestBody Role role){
        return roleService.updateRole(role);
    }

    /**
     * 根绝ID获取角色信息
     */
    @PreAuthorize("hasAuthority('noahplat-service-user:RoleController:getRoleById')")
    @GetMapping("/getRoleById")
    public Result getRoleById(@NotNull(message = "角色ID不能为空 ") @Min(value = 1, message = "角色Id不能为空")Long id){
        return roleService.getRoleById(id);
    }

    /**
     * 根据ID删除角色
     */
    @PreAuthorize("hasAuthority('noahplat-service-user:RoleController:delRole')")
    @DeleteMapping("/delRole")
    public Result delRole(Long[] id){
        return roleService.delRole(id);
    }

    /**
     * 关联权限修改
     * @return
     */
    @PreAuthorize("hasAuthority('noahplat-service-user:RoleController:updateRoles')")
    @PutMapping("/updateRoles")
    public Result updateRoles(@RequestBody RoleDao roleDao){
        return roleService.updateRoles(roleDao);
    }
}
